package com.ljx.Controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ljx.Domain.Category;
import com.ljx.Domain.Dish;
import com.ljx.Domain.DishFlavor;
import com.ljx.Domain.R;
import com.ljx.Dto.DishDto;
import com.ljx.Service.CategoryService;
import com.ljx.Service.DishFlavorService;
import com.ljx.Service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("dish")
public class DishController {
    @Autowired
    DishService dishService;
    @Autowired
    CategoryService categoryService;
    @Autowired
    DishFlavorService dishFlavorService;
    @Autowired
    RedisTemplate redisTemplate;

    /**
     * 添加菜品
     *
     * @param dishDto
     * @return
     */
    @PostMapping
    public R<String> add(@RequestBody DishDto dishDto) {
        dishService.addDish(dishDto);
        //根据id删除单个菜品数据
        String key = "Dish_"+dishDto.getCategoryId()+"_1";
        redisTemplate.delete(key);
        return R.success("添加成功");
    }

    /**
     * 展示菜品
     *
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("page")
    public R<Page<DishDto>> pageDish(int page, int pageSize, String name) {
        //创建Page对象
        Page<Dish> pages = new Page<>(page, pageSize);
        //创建条件构造器
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        //添加模糊查询
        wrapper.like(name != null, Dish::getName, name);
        //添加排序
        wrapper.orderByAsc(Dish::getCreateTime);
        dishService.page(pages, wrapper);
        Page<DishDto> pageDishDto = new Page<>();
        //复制对象到pageDishDto
        BeanUtils.copyProperties(pages, pageDishDto, "records");
        List<Dish> records = pages.getRecords();
        //使用stream流改变records数据
        List<DishDto> list = records.stream().map((item) -> {
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(item, dishDto);
            Long categoryId = item.getCategoryId();
            //查询category表
            Category category = categoryService.getById(categoryId);
            //将名字保存进dishDto
            String categoryName = category.getName();
            dishDto.setCategoryName(categoryName);
            return dishDto;
        }).collect(Collectors.toList());
        pageDishDto.setRecords(list);
        return R.success(pageDishDto);
    }

    /**
     * 修改菜品查询
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<DishDto> get(@PathVariable Long id) {
        //根据id查询
        DishDto dishDto = dishService.getByIdDish(id);
        return R.success(dishDto);
    }

    /**
     * 修改菜品
     *
     * @param dishDto
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody DishDto dishDto) {
        dishService.updateDish(dishDto);
        //根据id删除单个菜品数据
        String key = "Dish_"+dishDto.getCategoryId()+"_1";
        redisTemplate.delete(key);
        return R.success("修改成功");
    }

    /**
     * 新增套餐查询
     *
     * @param dish
     * @return
     */
    @GetMapping("list")
    public R<List<DishDto>> list(Dish dish) {
        //保存到redis的名字
        String key = "Dish_" + dish.getCategoryId() + "_" + dish.getStatus();
        List<DishDto> dishDtoList = null;
        //使用redis保存数据
        ValueOperations valueRedis = redisTemplate.opsForValue();
        //判断redis里是否有数据
        dishDtoList = (List<DishDto>) valueRedis.get(key);
        if (dishDtoList!=null) {
            //说明redis有数据，直接返回
            return R.success(dishDtoList);
        }
        //没有数据走mysql数据库
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Dish::getCategoryId, dish.getCategoryId());
        wrapper.eq(dish.getStatus() != null, Dish::getStatus, dish.getStatus());
        wrapper.orderByAsc(Dish::getUpdateTime);
        List<Dish> dishList = dishService.list(wrapper);
        //添加爱好
        //使用stream流改变dishDtoList数据
        dishDtoList = dishList.stream().map((item) -> {
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(item, dishDto);
            //根据dishId查询数据
            Long dishId = item.getId();
            LambdaQueryWrapper<DishFlavor> wrapper1 = new LambdaQueryWrapper<>();
            wrapper1.eq(DishFlavor::getDishId, dishId);
            List<DishFlavor> dishFlavorList = dishFlavorService.list(wrapper1);
            dishDto.setFlavors(dishFlavorList);
            return dishDto;
        }).collect(Collectors.toList());
        //添加数据到redis中,一小时之后自动删除
        valueRedis.set(key,dishDtoList,1, TimeUnit.HOURS);
        return R.success(dishDtoList);
    }
}
